/**
 * 公共JS.
 * User: zhaoming
 * Date: 15-8-18
 * Time: 上午11:47
 * To change this template use File | Settings | File Templates.
 */

/**
 * 打开导航菜单
 * @param elementId 当前菜单ID
 * @param navId 父菜单ID
 */
function activeNavBar(elementId, navId){
    if(elementId != ""){
        $("#"+elementId+"").addClass("active");
    }
    if(navId != ""){
        $("#"+navId+"").addClass("active open");
    }
}

$(document).ready(function() {
    Date.prototype.format = function(format) {

        var o = {
            "M+" :this.getMonth() + 1,
            "d+" :this.getDate(),
            "h+" :this.getHours(),
            "m+" :this.getMinutes(),
            "s+" :this.getSeconds(),
            "q+" :Math.floor((this.getMonth() + 3) / 3),
            "S" :this.getMilliseconds()
        };

        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "")
                .substr(4 - RegExp.$1.length));
        }

        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
                    : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    };

});

/**
 * 修改jqGrid图标
 * @param table
 */
function updatePagerIcons(table) {
    var replacement = {
        'ui-icon-seek-first' : 'ace-icon fa fa-angle-double-left bigger-140',
        'ui-icon-seek-prev' : 'ace-icon fa fa-angle-left bigger-140',
        'ui-icon-seek-next' : 'ace-icon fa fa-angle-right bigger-140',
        'ui-icon-seek-end' : 'ace-icon fa fa-angle-double-right bigger-140'
    };
    $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function(){
        var icon = $(this);
        var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

        if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
    })
}

/**
 * 按钮提示样式
 * @param table
 */
function enableTooltips(table) {
    $('.navtable .ui-pg-button').tooltip({container:'body'});
    $(table).find('.ui-pg-div').tooltip({container:'body'});
}

/**
 * 遮罩层
 */
Loading = (function(){
    return {
        show : function(){
            $("#loadWindow").show();
            $("#fakeloader").fakeLoader();
            $("#fakeloader").show();
        },
        hide : function(){
            $("#loadWindow").fadeOut();
            $("#fakeloader").fadeOut();
        }
    }
})();


/**
 * 多级联动组件
 * @type {{init, load, clear}}
 */
Linkage = (function(){
    var _data = [];
    return {
        init : function(linkageArray){
            _data = linkageArray;
            if(linkageArray != null){
                //绑定事件
                for(var i=0; i<linkageArray.length; i++){
                    var linkage = linkageArray[i];

                    if(linkage.parent == ""){
                        Linkage.load(0, linkage);
                    }

                    if(i < linkageArray.length-1){
                        $("#"+linkageArray[i].id).bind("change", {index: i}, function(event) {
                            var index = event.data.index;
                            var linkage = linkageArray[index + 1]
                            var data = {};
                            data[linkage.parent] = $(this).val();
                            linkage.data = data;
                            Linkage.load(index, linkageArray[index + 1]);
                        });
                    }
                }
            }
        },
        load : function(index, linkage){
            var id = linkage.id;
            var url = linkage.url;
            var data = linkage.data || {};
            var code = linkage.code || "code";
            var name = linkage.name || "name";
            $.ajax({
                url: url,
                dataType: "json",
                type: "POST",
                data: data,
                success: function (data) {
                    Linkage.clear(index);
                    for (var i = 0; i < data.length; i++) {
                        $("#"+id).append("<option value='" + data[i][code] + "'>" + data[i][name] + "</option>");
                    }
                },
                error: function(data){

                }
            });
        },
        clear: function(index){
            var i = index + 1;
            while(i < _data.length){
                var linkage = _data[i];
                $("#"+linkage.id).empty();
                $("#"+linkage.id).append("<option value=''>请选择</option>");
                $("#"+linkage.id).select2("val", "");
                i++;
            }
        }
    }
})();

Commons = (function() {
    return {
        /**
         * 弹出窗口
         *
         * @param _context_ 内容
         * @param _title_ 标题
         * @param _call_back_ 回调函数
         */
        alert : function(_context_, _title_, _call_back_) {
            if (undefined == _title_) {
                _title_ = "操作提示";
            }else{
                if ($.isFunction(_title_)) {
                    _call_back_ = _title_;
                    _title_ = "操作提示";

                }
            }

            bootbox.dialog({
                title: _title_,
                message: _context_,
                buttons:
                {
                    "close" :{
                        "label" : "关闭",
                        "className" : "button button-primary button-rounded button-small",
                        "callback": function() {
                            if(_call_back_ != undefined){
                                _call_back_();
                            }
                        }
                    }
                }
            });
        },

        /**
         * 普通提示
         *
         * @param _context_ 内容
         * @param _title_ 标题
         * @param _call_back_ 回调函数
         */
        showInfo : function(_context_, _title_, _call_back_) {
            if (undefined == _title_) {
                _title_ = "信息提示";
            }else{
                if ($.isFunction(_title_)) {
                    _call_back_ = _title_;
                    _title_ = "信息提示";

                }
            }
            _context_= "<i class='ace-icon fa-info-circle fa-lg blue'></i> <span class='bigger-110'>" + _context_ +"</span>";
            Commons.alert(_context_, _title_, _call_back_);
        },

        /**
         * 错误提示
         *
         * @param _context_ 内容
         * @param _title_ 标题
         * @param _call_back_ 回调函数
         */
        showError : function(_context_, _title_, _call_back_) {
            if (undefined == _title_) {
                _title_ = "错误提示";
            }else{
                if ($.isFunction(_title_)) {
                    _call_back_ = _title_;
                    _title_ = "错误提示";

                }
            }
            _context_= "<i class='ace-icon fa fa-minus-circle fa-lg red'></i> <span class='bigger-110'>" + _context_ +"</span>";
            Commons.alert(_context_, _title_, _call_back_);
        },

        /**
         * 警告提示
         *
         * @param _context_ 内容
         * @param _title_ 标题
         * @param _call_back_ 回调函数
         */
        showWarn : function(_context_, _title_, _call_back_) {
            if (undefined == _title_) {
                _title_ = "警告提示";
            }else{
                if ($.isFunction(_title_)) {
                    _call_back_ = _title_;
                    _title_ = "警告提示";

                }
            }
            _context_= "<i class='ace-icon fa fa-exclamation-circle fa-lg orange'></i> <span class='bigger-110'>" + _context_ +"</span>";
            Commons.alert(_context_, _title_, _call_back_);
        },

        /**
         * 成功提示
         *
         * @param _context_ 内容
         * @param _title_ 标题
         * @param _call_back_ 回调函数
         */
        showSuccess : function(_context_, _title_, _call_back_) {
            if (undefined == _title_) {
                _title_ = "成功提示";
            }else{
                if ($.isFunction(_title_)) {
                    _call_back_ = _title_;
                    _title_ = "成功提示";

                }
            }
            _context_= "<i class='ace-icon fa fa-check-circle fa-lg green'></i> <span class='bigger-110'>" + _context_ +"</span>";
            Commons.alert(_context_, _title_, _call_back_);
        },

        /**
         * 确认提示
         *
         * @param _context_ 内容
         * @param _title_ 标题
         * @param _ok_call_back_ 确定回调函数
         * @param _cancel_call_back_ 取消回调函数
         */
        showConfirm : function(_context_, _title_, _ok_call_back_, _cancel_call_back_) {
            if (undefined == _title_) {
                _title_ = "<b>确认提示</b>";
            }else{
                if ($.isFunction(_title_)) {
                    _cancel_call_back_ = _ok_call_back_;
                    _ok_call_back_ = _title_;
                    _title_ = "<b>确认提示</b>";

                }
            }
            _context_= "<i class='ace-icon fa fa-question-circle fa-lg blue'></i> <span class='bigger-110'>" + _context_ +"</span>";
            bootbox.dialog({
                title: _title_,
                message: _context_,
                buttons:
                {
                    "ok" :{
                        "label" : "确定",
                        "className" : "button button-primary button-rounded button-small",
                        "callback": function() {
                            if(_ok_call_back_ != undefined){
                                _ok_call_back_();
                            }
                        }
                    },
                    "cancel": {
                        "label" : "取消",
                        "className" : "button button-action  button-rounded button-small",
                        "callback": function() {
                            if(_cancel_call_back_ != undefined){
                                _cancel_call_back_();
                            }
                        }
                    }
                }
            });
        }
    }
})();


